//
//  LLBShopDetailMessageCell.m
//  FLLTB
//
//  Created by apple on 2019/7/12.
//  Copyright © 2019 LLB. All rights reserved.
//
#define Test_Message_Font_Size 16
#import "LLBShopDetailMessageCell.h"

@implementation LLBShopDetailMessageCell
+ (CGSize)sizeForMessageModel:(RCMessageModel *)model
      withCollectionViewWidth:(CGFloat)collectionViewWidth
         referenceExtraHeight:(CGFloat)extraHeight {
    LLBDetailMessage *message = (LLBDetailMessage *)model.content;
//    CGSize size = [LLBShopDetailMessageCell getBubbleBackgroundViewSize:message];
    
    //    CGFloat __messagecontentview_height = size.height;
    CGFloat __messagecontentview_height = 150;
    __messagecontentview_height += extraHeight;
    
    //    return CGSizeMake(kScreenWidth, 120);
    
    return CGSizeMake(collectionViewWidth, __messagecontentview_height);
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
//    self.bubbleBackgroundView = [[UIImageView alloc] initWithFrame:CGRectZero];
//    self.bubbleBackgroundView.image = [UIImage imageNamed:@"红包背景"];
//    [self.messageContentView addSubview:self.bubbleBackgroundView];
    
    self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 20)];
    [self.textLabel setFont:[UIFont systemFontOfSize:Test_Message_Font_Size]];
    self.textLabel.text = @"正在浏览商品···";
    
    self.textLabel.numberOfLines = 0;
    [self.textLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [self.textLabel setTextAlignment:NSTextAlignmentLeft];
//    [self.textLabel setTextColor:[UIColor redColor]];
    [self.messageContentView addSubview:self.textLabel];
    
    
    self.messageContentView.backgroundColor = [UIColor whiteColor];
    self.messageContentView.layer.masksToBounds = YES;
    self.messageContentView.layer.cornerRadius = 10;
    
    //图
    self.leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 40, 58, 58)];
    self.leftImageView.backgroundColor = [UIColor  whiteColor];
    [self.messageContentView addSubview:self.leftImageView];
    
    //优惠券
    self.couponLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 40, 120, 20)];
    self.couponLabel.font = [UIFont systemFontOfSize:13];
    self.couponLabel.textColor = [UIColor blackColor];
    self.couponLabel.text = @"优惠券:  80元";
    [self.messageContentView addSubview:self.couponLabel];
//
//    //返现
//    self.reurnMoneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 85, 120, 20)];
//    self.reurnMoneyLabel.font = [UIFont systemFontOfSize:13];
//    self.reurnMoneyLabel.textColor = [UIColor redColor];
//    self.reurnMoneyLabel.text = @"返现:  80元";
//    [self.messageContentView addSubview:self.reurnMoneyLabel];
    
    //原价
//    self.originalPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 110, 100, 20)];
//    self.originalPriceLabel.font = KFont(11);
//    self.originalPriceLabel.textColor = kColor(153, 153, 153);
//    self.originalPriceLabel.text = @"";
//    [self.messageContentView addSubview:self.originalPriceLabel];
    
    //现价
    self.priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 65, 120, 20)];
    self.priceLabel.font = [UIFont systemFontOfSize:18];
    self.priceLabel.textColor = [UIColor colorWithHexString:@"#00C8AC"];
    self.priceLabel.text = @"¥ 80";
    [self.messageContentView addSubview:self.priceLabel];
    
    
    //下单
//    self.goOrderButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 150, 100, 30)];
//    self.goOrderButton.layer.masksToBounds = YES;
//    self.goOrderButton.layer.cornerRadius = 15;
//    [self.goOrderButton setTitle:@"去下单" forState:0];
//    [self.goOrderButton setTitleColor:[UIColor redColor] forState:0];
//    self.goOrderButton.titleLabel.font = [UIFont systemFontOfSize:14];
//    self.goOrderButton.backgroundColor = [UIColor whiteColor];
//    [self.goOrderButton addTarget:self action:@selector(goOrderAction) forControlEvents:UIControlEventTouchUpInside];
//    [self.messageContentView addSubview:self.goOrderButton];
    
    //d分享
    self.shareButton = [[UIButton alloc]initWithFrame:CGRectMake(125, 105, 70, 26)];
    self.shareButton.layer.masksToBounds = YES;
    self.shareButton.layer.cornerRadius = 13;
    self.shareButton.layer.borderWidth = 1;
    self.shareButton.layer.borderColor = [UIColor colorWithHexString:@"#00DAAF"].CGColor;
    self.shareButton.backgroundColor = [UIColor colorWithHexString:@"#00DAAF"];
    [self.shareButton setTitle:@"立即推送" forState:0];
    [self.shareButton addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    [self.shareButton setTitleColor:[UIColor whiteColor] forState:0];
    self.shareButton.titleLabel.font = [UIFont systemFontOfSize:14];
//    self.shareButton.backgroundColor = kRedColor;
    [self.messageContentView addSubview:self.shareButton];
    

    
    self.bubbleBackgroundView.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longPress =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    [self.bubbleBackgroundView addGestureRecognizer:longPress];
    
    UITapGestureRecognizer *textMessageTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTextMessage:)];
    textMessageTap.numberOfTapsRequired = 1;
    textMessageTap.numberOfTouchesRequired = 1;
    [self.messageContentView addGestureRecognizer:textMessageTap];
    self.messageContentView.userInteractionEnabled = YES;
}

- (void)tapTextMessage:(UIGestureRecognizer *)gestureRecognizer {
    if ([self.delegate respondsToSelector:@selector(didTapMessageCell:)]) {
        [self.delegate didTapMessageCell:self.model];
    }
}

- (void)setDataModel:(RCMessageModel *)model {
    [super setDataModel:model];
    
    [self setAutoLayout];
}

- (void)setAutoLayout {
    LLBDetailMessage *testMessage = (LLBDetailMessage *)self.model.content;
    if (testMessage) {
        //        self.textLabel.text = testMessage.money;
    }
    NSLog(@"商品详情%@",testMessage.detail);
//    CGSize textLabelSize = [[self class] getTextLabelSize:testMessage];
    //    CGSize bubbleBackgroundViewSize = [[self class] getBubbleSize:textLabelSize];
    CGSize bubbleBackgroundViewSize = CGSizeMake(kScreenWidth/2, 80);
    
    CGRect messageContentViewRect = self.messageContentView.frame;
    
    NSLog(@"%lu",(unsigned long)self.messageDirection);
    //拉伸图片
    if (MessageDirection_RECEIVE == self.messageDirection) {
        
//        self.textLabel.text = [NSString stringWithFormat:@"%@",testMessage.detail[@"itemTitle"]];
        
        
        [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:testMessage.detail[@"itemPictUrl"]] placeholderImage:[UIImage imageNamed:@"商品占位图"]];
        
//        self.couponLabel.text = [NSString stringWithFormat:@"优惠券: %@元",testMessage.detail[@"couponMoney"]];
        
//        NSMutableAttributedString *couponString = [[NSMutableAttributedString alloc]initWithString:@"优惠券:"];
//
//        [couponString appendAttributedString:[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@元",testMessage.detail[@"couponMoney"]] attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13], NSForegroundColorAttributeName:[UIColor redColor]}]];
        
//        [couponString appendAttributedString:[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"到账"] attributes:@{NSFontAttributeName: KPFFont(11), NSForegroundColorAttributeName:kColor(153, 153, 153)}]];
        
        self.couponLabel.text = [NSString stringWithFormat:@"%@",testMessage.detail[@"itemTitle"]];
        
        
//        self.reurnMoneyLabel.text = [NSString stringWithFormat:@"返现: %@元",testMessage.detail[@"tkMoney"]];
        
        NSMutableAttributedString *reurnMoneyString = [[NSMutableAttributedString alloc]initWithString:@"返现:"];
        
        [reurnMoneyString appendAttributedString:[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@元",testMessage.detail[@"tkMoney"]] attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13], NSForegroundColorAttributeName:[UIColor redColor]}]];
        
        self.reurnMoneyLabel.attributedText = reurnMoneyString;
        
//        self.originalPriceLabel.text = [NSString stringWithFormat:@"¥%@",testMessage.detail[@"itemPrice"]];
        
        self.priceLabel.text = [NSString stringWithFormat:@"¥%@",testMessage.detail[@"itemEndPrice"]];
        
        
    } else {
        //        self.textLabel.text = [NSString stringWithFormat:@"%@",testMessage.detail[@"itemTitle"]];
                
                
                [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:testMessage.detail[@"itemPictUrl"]] placeholderImage:[UIImage imageNamed:@"商品占位图"]];
                
        //        self.couponLabel.text = [NSString stringWithFormat:@"优惠券: %@元",testMessage.detail[@"couponMoney"]];
                
        //        NSMutableAttributedString *couponString = [[NSMutableAttributedString alloc]initWithString:@"优惠券:"];
        //
        //        [couponString appendAttributedString:[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@元",testMessage.detail[@"couponMoney"]] attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13], NSForegroundColorAttributeName:[UIColor redColor]}]];
                
        //        [couponString appendAttributedString:[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"到账"] attributes:@{NSFontAttributeName: KPFFont(11), NSForegroundColorAttributeName:kColor(153, 153, 153)}]];
                
                self.couponLabel.text = [NSString stringWithFormat:@"%@",testMessage.detail[@"itemTitle"]];
                
                
        //        self.reurnMoneyLabel.text = [NSString stringWithFormat:@"返现: %@元",testMessage.detail[@"tkMoney"]];
                
                NSMutableAttributedString *reurnMoneyString = [[NSMutableAttributedString alloc]initWithString:@"返现:"];
                
                [reurnMoneyString appendAttributedString:[[NSAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@元",testMessage.detail[@"tkMoney"]] attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13], NSForegroundColorAttributeName:[UIColor redColor]}]];
                
                self.reurnMoneyLabel.attributedText = reurnMoneyString;
                
        //        self.originalPriceLabel.text = [NSString stringWithFormat:@"¥%@",testMessage.detail[@"itemPrice"]];
                
                self.priceLabel.text = [NSString stringWithFormat:@"¥%@",testMessage.detail[@"itemEndPrice"]];
    }
}
//-(void)goOrderAction{
//    
////    if (self.cellButtonType) {
////        self.cellButtonType(1,@"");
////    }
//    LLBDetailMessage *testMessage = (LLBDetailMessage *)self.model.content;
//    if ([self.delegate respondsToSelector:@selector(goOrderTableViewCellNeedDelete:)]) {
//        [self.delegate goOrderTableViewCellNeedDelete:testMessage.detail];
//    }
//
//}
-(void)shareAction{
    
    LLBDetailMessage *testMessage = (LLBDetailMessage *)self.model.content;
    if ([self.delegate respondsToSelector:@selector(shareTableViewCellNeedDelete:)]) {
        [self.delegate shareTableViewCellNeedDelete:testMessage.detail];
    }
}
- (void)longPressed:(id)sender {
    UILongPressGestureRecognizer *press = (UILongPressGestureRecognizer *)sender;
    if (press.state == UIGestureRecognizerStateEnded) {
        return;
    } else if (press.state == UIGestureRecognizerStateBegan) {
        [self.delegate didLongTouchMessageCell:self.model inView:self.bubbleBackgroundView];
    }
}

//+ (CGSize)getTextLabelSize:(LLBDetailMessage *)message {
//    if ([message.title length] > 0) {
//        float maxWidth = kScreenWidth -
//        (10 + [RCIM sharedRCIM].globalMessagePortraitSize.width + 10) * 2 - 5 - 35;
//        CGRect textRect = [message.title
//                           boundingRectWithSize:CGSizeMake(maxWidth, 8000)
//                           options:(NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin |
//                                    NSStringDrawingUsesFontLeading)
//                           attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:Test_Message_Font_Size]}
//                           context:nil];
//        textRect.size.height = ceilf(textRect.size.height);
//        textRect.size.width = ceilf(textRect.size.width);
//        return CGSizeMake(textRect.size.width + 5, textRect.size.height + 5);
//    } else {
//        return CGSizeZero;
//    }
//}

+ (CGSize)getBubbleSize:(CGSize)textLabelSize {
    CGSize bubbleSize = CGSizeMake(textLabelSize.width, textLabelSize.height);
    
    if (bubbleSize.width + 12 + 20 > 50) {
        bubbleSize.width = bubbleSize.width + 12 + 20;
    } else {
        bubbleSize.width = 50;
    }
    if (bubbleSize.height + 7 + 7 > 40) {
        bubbleSize.height = bubbleSize.height + 7 + 7;
    } else {
        bubbleSize.height = 40;
    }
    
    return bubbleSize;
}

//+ (CGSize)getBubbleBackgroundViewSize:(LLBDetailMessage *)message {
//    CGSize textLabelSize = [[self class] getTextLabelSize:message];
//    return [[self class] getBubbleSize:textLabelSize];
//}
@end
